package Rt

import "fmt"

var Pager HxPager

func init() {
	Pager = HxPager{}
}

type HxPager struct {

}

func (_ HxPager) Calculate(current int, size int, all int) map[string]int {
	res := make(map[string]int)
	res["current"] = current
	res["size"] = size
	res["all"] = all
	res["pages"] = all/size
	if all%size != 0 {
		res["pages"] +=1
	}
	res["end"] = current*size
	if res["end"] > all {
		res["end"] = all
	}
	res["begin"] = (current - 1)*size
	if res["begin"] < 1 {
		res["begin"] = 1
	}
	if res["begin"] > all {
		res["begin"] = all
	}
	return res
}

func (_ HxPager) Html(pageMap map[string]int, url string, gap int) string {
	html := `<span class="total">` + fmt.Sprint(pageMap["begin"]) + " / " + fmt.Sprint(pageMap["end"]) + " in " + fmt.Sprint(pageMap["all"]) + `</span>`
	for i := 1; i <= pageMap["pages"]; i++ {
		if i > 1 {
			if gap > 0 {
				if i < pageMap["current"] - gap {
					html += ".&nbsp;"
					continue
				}
				if i > pageMap["current"] + gap {
					if i != pageMap["pages"] {
						html += ".&nbsp;"
						continue
					}
				}
			}
		}
		if i == pageMap["current"] {
			html += `<a class="active item" href="` + url + `page/` + fmt.Sprint(i) + `">` + fmt.Sprint(i) + "</a>"
		}else {
			html += `<a class="item" href="` + url + `page/` + fmt.Sprint(i) + `">` + fmt.Sprint(i) + "</a>"
		}
	}
	return html
}

